package com.phpxiu.app.view.popupwindow;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.utils.KKYUtil;

/**
 * Created by WuJinZhou on 2016/5/7.
 */
public class ViewTopPop extends PopupWindow implements View.OnTouchListener{
    private View rootView;
    public final static String TAG="ViewTopPop";
    private TextView title;
    private int mPopWidth=0;
    private int mPopHeight=0;
    public ViewTopPop(Context context) {
        super(context);
        rootView= LayoutInflater.from(context).inflate(R.layout.view_top_pop_win,null);
        title=(TextView)rootView.findViewById(R.id.title);
        rootView.setOnTouchListener(this);
        setContentView(rootView);
        mPopWidth= KKYUtil.dip2px(context, 120);
        mPopHeight=KKYUtil.dip2px(context,45);
        setWidth(mPopWidth);
        setHeight(mPopHeight);
        setBackgroundDrawable(new ColorDrawable());
        setTouchable(true);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(v.getId()== R.id.view_top_pop_win&&event.getAction()==MotionEvent.ACTION_DOWN){
            dismiss();
        }
        return false;
    }

    public void showAtLocation(View parent,String msg,Rect rect,int screeWidth) {
        int currentPopX=(screeWidth-mPopWidth)/2;
        int lastX=rect.left+(rect.width()-mPopWidth)/2;
        int offsetX=currentPopX-lastX;
        title.setText(msg);
        showAtLocation(parent, Gravity.TOP, -offsetX, rect.top - mPopHeight);
    }
}
